package com.googlecode.theta.server;

import org.mortbay.jetty.servlet.FilterHolder;

import javax.servlet.*;
import java.io.IOException;
import java.awt.*;


public class Main {
  private final static String APPLICATION_PATH = "./app";
  private final static String SERVER_PACKAGE_PATH = APPLICATION_PATH + "/server";
  private final static String SERVER_MAIN_MODULE = SERVER_PACKAGE_PATH + "/Main.js";
  private static final int PORT = 8080;

  public static void main(String[] args) throws Exception {
    final ScriptEngine engine = new ScriptEngine();
    final WebServer server = new WebServer(PORT, "./app/public");

    engine.addObject("Console", System.out);
    engine.addObject("Server", server);
    engine.addObject("Script", new ScriptHelper(engine, "./app/server"));
    engine.run("./app/server/Main.js");

    server.getContext().addFilter(new FilterHolder(new Filter() {
      public void doFilter(ServletRequest request,
                           ServletResponse response, FilterChain chain)
          throws IOException, ServletException {
        engine.invoke("main", new Object[]{request, response, chain});
      }
      public void destroy() {
      }
      public void init(FilterConfig filterConfig) throws ServletException {
      }
    }), "/*", 0);
    server.run();
  }
}
